package com.sali.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.sali.entity.UploadOssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@EnableConfigurationProperties(UploadOssProperties.class)
public class SaliUploadOssService {

    @Autowired
    private UploadOssProperties uploadOssProperties;

    public String uploadfile(File multipartFile) {
        String bucketName = uploadOssProperties.getBucketName();
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(uploadOssProperties.getEndpoint(), uploadOssProperties.getAccessKeyId(), uploadOssProperties.getAccessKeySecret());
            if (!ossClient.doesBucketExist(bucketName)) {
                //创建bucket
                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限：公共读
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }

            // 2: 获取文件上传的流
            InputStream inputStream = new FileInputStream(multipartFile);
            // 3：构建日期目录
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String datePath = dateFormat.format(new Date());// 日期目录：2021/10/27

            // 4: 获取文件名
            String originname = multipartFile.getName();
            String filename = UUID.randomUUID().toString();
            String suffix = originname.substring(originname.lastIndexOf("."));
            String newName = filename + suffix;
            String fileUrl = datePath + "/" + newName;
            //5：文件上传到阿里云服务器
            ossClient.putObject(bucketName, fileUrl, inputStream);
            return "https://" + bucketName + "." + uploadOssProperties.getEndpoint() + "/" + fileUrl;
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        } finally {
            ossClient.shutdown();
        }

    }

    public String uploadfile(MultipartFile multipartFile, String dir) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(uploadOssProperties.getEndpoint(), uploadOssProperties.getAccessKeyId(), uploadOssProperties.getAccessKeySecret());

            // 2: 获取文件上传的流
            InputStream inputStream = multipartFile.getInputStream();
            // 3：构建日期目录
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String datePath = dateFormat.format(new Date());// 日期目录：2021/10/27

            // 4: 获取文件名
            String originname = multipartFile.getOriginalFilename();
            String filename = UUID.randomUUID().toString();
            String suffix = originname.substring(originname.lastIndexOf("."));
            String newName = filename + suffix;
            String fileUrl = dir + "/" + datePath + "/" + newName;
            //5：文件上传到阿里云服务器
            ossClient.putObject(uploadOssProperties.getBucketName(), fileUrl, inputStream);

            return "https://" + uploadOssProperties.getBucketName() + "." + uploadOssProperties.getEndpoint() + "/" + fileUrl;
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        } finally {
            ossClient.shutdown();
        }

    }

}
